//
//  RootViewController.m
//  CoreData通讯录
//
//  Created by 邝子涵 on 15/11/20.
//  Copyright © 2015年 kuangzihan. All rights reserved.
//

#import "RootViewController.h"
#import "MyCell.h"
#import "SecondViewController.h"
#import "CoreDataManager.h"
#import "Student.h"
#import "ReviseViewController.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate, SecondViewControllerDelegate, ReviseViewControllerDelegate>
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *array;
@property(nonatomic, assign) NSInteger index;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加联系人" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    
    // 给导航控制器右侧添加一个编辑按钮
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    self.editButtonItem.title = @"编辑";

    
    [self createTableView];
    
    [self createData];
}


#pragma mark - tableView编辑方法
// 重写重写系统的编辑按钮出发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    // 用super调用父类方法
    [super setEditing:editing animated:animated];
    // 如果父类看到我重写会先执行我写的方法
    [self.tableView setEditing:editing animated:animated];
}

// 设置哪些可以进行编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    // 这个方法默认就是所有行都可以进行编辑
    return YES;
}

// 修改删除按钮
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}



// 使用范围2.0-7.0 7.0之后该属性被废弃
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    Student *stu = self.array[indexPath.row];
    // 可以写多个按钮,通过重写block的方式实现按钮功能
    UITableViewRowAction *firstAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"编辑");
        ReviseViewController *reviseVC = [[ReviseViewController alloc] init];
        [self.navigationController pushViewController:reviseVC animated:YES];
        reviseVC.reviseDelegate = self;
        
        Student *stu = self.array[indexPath.row];
        reviseVC.stu = stu;
        NSLog(@"%ld", indexPath.row);
        self.index = indexPath.row;
        NSLog(@"11111111%ld", self.index);
    }];
    UITableViewRowAction *secondAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        // 要实现的功能,都写在block里
        NSLog(@"删除");
        // 删除一定要删除对应数据源的数据
        [[CoreDataManager defaults] deleteName:stu.name];
        // 删除同时删除对应数组的数组,实现同步删除
        [self.array removeObject:stu];
        
        [self.tableView reloadData];
    }];
    
    
    firstAction.backgroundColor = [UIColor blueColor];
    
    return @[firstAction, secondAction];
}



#pragma mark - 数据解析
- (void)createData{
    NSArray *array = [[CoreDataManager defaults] slect];
    self.array = [NSMutableArray arrayWithArray:array];
    
    [self.tableView reloadData];
}



#pragma mark - 创建tableView
- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    // 注册tabelView
    [self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCellReuseIdentifier"];
}



#pragma mark - UIbarButton点击方法
- (void)click:(UIButton *)button{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    secondVC.secondDelegate = self;
    
    [self.navigationController pushViewController:secondVC animated:YES];
}

#pragma mark - tableView点击方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCellReuseIdentifier"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    Student *stu = self.array[indexPath.row];
    
    NSLog(@"%@", stu.name);
    cell.nameLabel.text = [NSString stringWithFormat:@"姓名: %@", stu.name];
    cell.phoneLabel.text = [NSString stringWithFormat:@"电话: %@", stu.phone];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 添加联系人协议方法
- (void)addAressBook:(Student *)student{
    [self.array addObject:student];
    [self.tableView reloadData];
}

#pragma mark - 修改联系人协议方法
- (void)reviseAddressBook:(Student *)stu{
    NSLog(@"1112222222%ld", self.index);

    
    [self.array insertObject:stu atIndex:self.index];
    [self.tableView reloadData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
